package com.nhjf.model.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.nhjf.command.system.CreateResourceCommand;
import com.nhjf.command.system.ModifyResourceCommand;
import com.nhjf.model.BaseModel;

/**
 * 
 * @类功能说明：权限资源
 *
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "resource")
public class Resource extends BaseModel{

	/**
	 * 主键
	 */
	@Id
	@GeneratedValue(generator = "generator")
	@GenericGenerator(name = "generator", strategy = "native")
	@Column(name = "id", unique = true)
	private Long id;
	
	/**
	 * 资源名称
	 */
	@Column(name = "name")
	private String name;
	
	/**
	 * 资源路径
	 */
	@Column(name = "url")
	private String url;
	
	/**
	 * 资源编号
	 */
	@Column(name = "number")
	private Integer number;
	
	/**
	 * 父资源ID
	 */
	@Column(name = "parentId")
	private Long parentId;
	
	/**
	 * 资源类型
	 * 1:模块
	 * 2:资源
	 */
	@Column(name = "type")
	private Long type;
	
	/**
	 * 创建人ID 登录用户所属员工的ID
	 */
	@Column(name = "createrUser")
	private Long createrUser;
	
	/**
	 * 创建时间
	 */
	@Column(name = "createDate")
	private Date createDate;
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "role_has_res" ,joinColumns = { @JoinColumn(name = "resource_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	private Set<Role> roleSet = new LinkedHashSet<Role>();
	
	@Transient
	private List<Resource> resourceList;

	public void createResource(CreateResourceCommand command){
		setName(command.getName());
		setUrl(command.getUrl());
		setParentId(command.getParentId());
		
		setNumber(command.getNumber());
		setType(command.getType());
		setCreateDate(new Date());
	}
	
	public void modifyResource(ModifyResourceCommand command){
		setName(command.getName());
		setUrl(command.getUrl());
		setNumber(command.getNumber());
		setType(command.getType());
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Long getType() {
		return type;
	}

	public void setType(Long type) {
		this.type = type;
	}

	public Long getCreaterUser() {
		return createrUser;
	}

	public void setCreaterUser(Long createrUser) {
		this.createrUser = createrUser;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	public List<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Resource> resourceList) {
		this.resourceList = resourceList;
	}
	


	
	
	
	
	
	
	
}
